#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int res=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){res=(res<<1)+(res<<3)+(c^48);c=getchar();}
	return res*f;
}
void write(int x)
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
struct node
{
	int nxt,to;
}e[114514<<1];
int head[114514],cnt_edge;
void add_edge(int u,int v)
{
	e[++cnt_edge].to=v;
	e[cnt_edge].nxt=head[u];
	head[u]=cnt_edge;
}
const int mod=111121;
int ksm(int x,int p)
{
	int res=1;
	while(p)
	{
		if(p&1) res=res*x%mod;
		x=x*x%mod;
		p>>=1;
	}
	return res;
}
int n,a[114514];
int ans;
int s;
map<int,int>mp[114514];
void dfs(int u,int fa,int f,int g)
{
	if(!f) return;
	if(!mp[s].count(u)) ans=(ans+ksm(f,g))%mod;
	mp[s][u]=mp[u][s]=1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u,f&a[v],g|a[v]);
	}
}
signed main()
{
	freopen("abstract.in","r",stdin);
	freopen("abstract.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<n;i++)
	{
		int u=read(),v=read();
		add_edge(u,v);add_edge(v,u);
	}
	for(int i=1;i<=n;i++) s=i,dfs(i,0,a[i],a[i]);
	write(ans);
	return 0;
}
